package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.service.seckill.SeckillGoodsService;
import com.qingcheng.service.seckill.SeckillOrderService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 岳剑飞
 * @Company http://www.ithiema.com
 */
@RestController
@RequestMapping("/seckill/order")
public class SeckillOrderController {
    @Reference
    private SeckillGoodsService seckillGoodsService;
    @Reference
    private SeckillOrderService seckillOrderService;


    /**
     * url: /seckill/order/add
     * 秒杀下单
     *
     * @param id
     * @return
     */
    @RequestMapping("/add")
    public Result add(String time,Long id) {

        try {
            //获取用户名
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            //如果用户是anonymousUser,则表明用户未登录
            if (username.equals("anonymousUser")) {
                //这里403错误代码,表示用户没登录
                return new Result(403, "请先登录! ");
            }
            //调用service增加订单
            Boolean aBoolean = seckillOrderService.add(id, time, username);

            if (aBoolean){
                System.out.println("测绘你公公了");
                //抢单成功
                return new Result(0,"下单成功! ");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(1, "秒杀下单失败! ");

    }

}
